# 使用Node.js 18作为构建阶段
FROM node:18-alpine as build-stage

# 设置工作目录
WORKDIR /app

# 复制package文件
COPY package*.json ./
COPY tsconfig.json ./
COPY vite.config.ts ./

# 安装依赖
RUN npm ci --only=production

# 复制源代码
COPY src ./src
COPY index.html ./
COPY public ./public

# 构建应用
RUN npm run build

# 使用Nginx作为生产环境
FROM nginx:alpine as production-stage

# 复制构建后的文件到Nginx
COPY --from=build-stage /app/dist /usr/share/nginx/html

# 复制自定义Nginx配置
COPY nginx.conf /etc/nginx/nginx.conf

# 暴露端口
EXPOSE 80

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
  CMD curl -f http://localhost/ || exit 1

# 启动Nginx
CMD ["nginx", "-g", "daemon off;"]